package com.xinke.module.system.controller.admin.user;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.user.*;
import com.xinke.module.system.model.entity.user.MenuDO;
import com.xinke.module.system.service.user.MenuService;

@Tag(name = "管理后台 - 菜单权限")
@RestController
@RequestMapping("/system/menu")
@Validated
public class MenuController {

    @Resource
    private MenuService menuService;

    @GetMapping("/list")
    @Operation(summary = "获得菜单权限列表")
    @PreAuthorize("@ss.hasPermission('system:menu:query')")
    public Result<List<MenuTreeVO>> getMemuList() {
        List<MenuTreeVO> menuTreeList = menuService.getMemuTreeList();
        return success(menuTreeList);
    }

    @PostMapping("/create")
    @Operation(summary = "创建菜单权限")
    @PreAuthorize("@ss.hasPermission('system:menu:create')")
    public Result<Long> createMenu(@Valid @RequestBody MenuTreeVO createReqVO) {
        return success(menuService.createMenu(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新菜单权限")
    @PreAuthorize("@ss.hasPermission('system:menu:update')")
    public Result<Boolean> updateMenu(@Valid @RequestBody MenuTreeVO updateReqVO) {
        menuService.updateMenu(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除菜单权限")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:menu:delete')")
    public Result<Boolean> deleteMenu(@PathVariable("id") Long id) {
        menuService.deleteMenu(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得菜单权限")
    @Parameter(name = "menuId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:menu:query')")
    public Result<MenuRespVO> getMenu(@RequestParam("menuId") Long menuId) {
        MenuDO menu = menuService.getMenu(menuId);
        return success(BeanUtils.toBean(menu, MenuRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得菜单权限分页")
    @PreAuthorize("@ss.hasPermission('system:menu:query')")
    public Result<PageResult<MenuRespVO>> getMenuPage(@Valid MenuPageReqVO pageReqVO) {
        PageResult<MenuDO> pageResult = menuService.getMenuPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MenuRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出菜单权限 Excel")
    @PreAuthorize("@ss.hasPermission('system:menu:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMenuExcel(@Valid MenuPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MenuDO> list = menuService.getMenuPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "菜单权限.xls", "数据", MenuRespVO.class,
                        BeanUtils.toBean(list, MenuRespVO.class));
    }

}